﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using YiTu.DBUtility;
namespace TOUR.SZHOME.DAL
{
    /// <summary>
    /// 数据表Landscape_Image的数据操作类
    /// </summary>
    public partial class Landscape_ImageDAL
    {
        #region ConstVariables
        private const string C_TABLE_NAME = "Landscape_Image";
        private const string C_SP_LANDSCAPE_IMAGE_FIELDS = "[XCID],[JQID],[TPLJ],[JQMC]";
        private const string C_SP_LANDSCAPE_IMAGE_INSERT = "INSERT INTO [Landscape_Image]([JQID],[TPLJ],[JQMC]) VALUES(@JQID,@TPLJ,@JQMC);SET @XCID = SCOPE_IDENTITY();";
        private const string C_SP_LANDSCAPE_IMAGE_UPDATE = "UPDATE [Landscape_Image] SET [JQID]=@JQID,[TPLJ]=@TPLJ,[JQMC]=@JQMC WHERE [XCID] = @XCID";
        private const string C_SP_LANDSCAPE_IMAGE_DELETE = "DELETE [Landscape_Image] WHERE [XCID] = @XCID";
        private const string C_SP_LANDSCAPE_IMAGE_GET = "SELECT [XCID],[JQID],[TPLJ],[JQMC] FROM [Landscape_Image] WHERE [XCID] = @XCID";
        #endregion

        private static string ConnectionString
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["TravelProject"].ConnectionString;
            }
        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        private Landscape_ImageDAL() { }

        /// <summary>
        /// 向数据表中插入一条新记录
        /// </summary>
        /// <param name="entity">Entity.Landscape_ImageEntity实体类</param>
        /// <remarks>如果表存在自增长字段，插入记录成功后自增长字段值会更新至实体</remarks>
        public static bool Insert(Entity.Landscape_ImageEntity entity)
        {
            List<SqlParameter> commandParms = new List<SqlParameter>();
            SqlParameter id_XCID = SqlHelper.CreateParam("@XCID", SqlDbType.Int, 0, ParameterDirection.Output, null);
            commandParms.Add(id_XCID);
            commandParms.Add(SqlHelper.CreateParam("@JQID", SqlDbType.Int, 0, ParameterDirection.Input, entity.JQID));
            commandParms.Add(SqlHelper.CreateParam("@TPLJ", SqlDbType.NVarChar, 200, ParameterDirection.Input, entity.TPLJ));
            commandParms.Add(SqlHelper.CreateParam("@JQMC", SqlDbType.NVarChar, 200, ParameterDirection.Input, entity.JQMC));

            return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, C_SP_LANDSCAPE_IMAGE_INSERT, commandParms) > 0 ? true : false;
            //entity.XCID = Convert.ToInt32(id_XCID.Value);
        }

        /// <summary>
        /// 获取数据库一条记录实体(根据主键条件)
        /// </summary>
        /// <param name="xCID">相册ID</param>
        /// <returns>Entity.Landscape_ImageEntity实体类</returns>
        public static Entity.Landscape_ImageEntity GetByXCID(int xCID)
        {
            Entity.Landscape_ImageEntity entity = null;
            List<SqlParameter> commandParms = new List<SqlParameter>();
            commandParms.Add(SqlHelper.CreateParam("@XCID", SqlDbType.Int, 0, ParameterDirection.Input, xCID));

            using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, C_SP_LANDSCAPE_IMAGE_GET, commandParms))
            {
                if (reader.Read())
                {
                    entity = ConvertToEntityFromDataReader(reader);
                }
            }

            return entity;
        }

        /// <summary>
        /// 更新数据库中一条记录(根据主键条件)
        /// </summary>
        /// <param name="entity">Entity.Landscape_ImageEntity实体类</param>
        public static bool Update(Entity.Landscape_ImageEntity entity)
        {
            List<SqlParameter> commandParms = new List<SqlParameter>();

            commandParms.Add(SqlHelper.CreateParam("@XCID", SqlDbType.Int, 0, ParameterDirection.Input, entity.XCID));
            commandParms.Add(SqlHelper.CreateParam("@JQID", SqlDbType.Int, 0, ParameterDirection.Input, entity.JQID));
            commandParms.Add(SqlHelper.CreateParam("@TPLJ", SqlDbType.NVarChar, 200, ParameterDirection.Input, entity.TPLJ));
            commandParms.Add(SqlHelper.CreateParam("@JQMC", SqlDbType.NVarChar, 200, ParameterDirection.Input, entity.JQMC));

            return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, C_SP_LANDSCAPE_IMAGE_UPDATE, commandParms) > 0 ? true : false;
        }

        /// <summary>
        /// 删除数据库中一条记录(根据主键条件)
        /// </summary>
        /// <param name="xCID">相册ID</param>
        public static bool Delete(int xCID)
        {
            List<SqlParameter> commandParms = new List<SqlParameter>();
            commandParms.Add(SqlHelper.CreateParam("@XCID", SqlDbType.Int, 0, ParameterDirection.Input, xCID));
            return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, C_SP_LANDSCAPE_IMAGE_DELETE, commandParms) > 0 ? true : false;
        }

        /// <summary>
        /// 按条件查询数据表,返回DataTable类型数据
        /// </summary>
        /// <param name="whereClause">SQL条件语句(可为空)，不须带"Where"关键字</param>
        /// <param name="dataFields">需返回字段(不能为空,为"*"则返回所有字段)</param>
        /// <param name="orderBy">SQL排序语句(不能为空)，不须带"Order By"关键字</param>
        /// <param name="startRowIndex">记录开始索引，从0开始</param>
        /// <param name="maximumRows">返回记录数量</param>
        /// <returns>DataTable</returns>
        public static DataTable SearchDT(string whereClause, string dataFields, string orderBy, int startRowIndex, int maximumRows)
        {
            if (dataFields.Trim() == "*")
            {
                dataFields = C_SP_LANDSCAPE_IMAGE_FIELDS;
            }
            return SqlListHepler.Search(ConnectionString, C_TABLE_NAME, dataFields, whereClause, orderBy, startRowIndex, maximumRows);
        }


        /// <summary>
        /// 按条件查询数据表,返回 Entity.Landscape_ImageEntity 数据集
        /// </summary>
        /// <param name="whereClause">SQL条件语句(可为空)，不须带"Where"关键字</param>
        /// <param name="orderBy">SQL排序语句(不能为空)，不须带"Order By"关键字</param>
        /// <param name="startRowIndex">记录开始索引，从0开始</param>
        /// <param name="maximumRows">返回记录数量</param>
        public static List<Entity.Landscape_ImageEntity> Search(string whereClause, string orderBy, int startRowIndex, int maximumRows)
        {
            List<Entity.Landscape_ImageEntity> list = new List<Entity.Landscape_ImageEntity>();
            using (SqlDataReader reader = SqlListHepler.SearchDataReader(ConnectionString, C_TABLE_NAME, C_SP_LANDSCAPE_IMAGE_FIELDS, whereClause, orderBy, startRowIndex, maximumRows))
            {
                while (reader.Read())
                {
                    list.Add(ConvertToEntityFromDataReader(reader));
                }
            }
            return list;
        }

        /// <summary>
        /// 按条件查询数据表,返回DataTable类型数据
        /// </summary>
        /// <param name="whereClause">SQL条件语句(可为空)，不须带"Where"关键字</param>
        /// <param name="dataFields">需返回字段(不能为空,为"*"则返回所有字段)</param>
        /// <param name="orderBy">SQL排序语句(可为空)，不须带"Order By"关键字</param>
        /// <param name="rowsToReturn">返回记录数量</param>
        /// <returns>DataTable</returns>
        public static DataTable SearchDT(string whereClause, string dataFields, string orderBy, int rowsToReturn)
        {
            if (dataFields.Trim() == "*")
            {
                dataFields = C_SP_LANDSCAPE_IMAGE_FIELDS;
            }
            return SqlListHepler.Search(ConnectionString, C_TABLE_NAME, dataFields, whereClause, orderBy, rowsToReturn);
        }

        /// <summary>
        /// 按条件查询数据表,返回 Entity.Landscape_ImageEntity 数据集
        /// </summary>
        /// <param name="whereClause">SQL条件语句(可为空)，不须带"Where"关键字</param>
        /// <param name="orderBy">SQL排序语句(可为空)，不须带"Order By"关键字</param>
        /// <param name="rowsToReturn">返回记录数量</param>
        public static List<Entity.Landscape_ImageEntity> Search(string whereClause, string orderBy, int rowsToReturn)
        {
            List<Entity.Landscape_ImageEntity> list = new List<Entity.Landscape_ImageEntity>();
            using (SqlDataReader reader = SqlListHepler.SearchDataReader(ConnectionString, C_TABLE_NAME, C_SP_LANDSCAPE_IMAGE_FIELDS, whereClause, orderBy, rowsToReturn))
            {
                while (reader.Read())
                {
                    list.Add(ConvertToEntityFromDataReader(reader));
                }
            }
            return list;
        }

        /// <summary>
        /// 按条件获取记录数量
        /// </summary>
        /// <param name="whereClause">SQL条件语句(可为空)，不须带"Where"关键字</param>
        /// <returns>int整型数据</returns>
        public static int SearchCount(string whereClause)
        {
            return SqlListHepler.SearchCount(ConnectionString, C_TABLE_NAME, whereClause);
        }

        /// <summary>
        /// 转换DataRow类型数据记录为实体
        /// </summary>
        private static Entity.Landscape_ImageEntity ConvertToEntityFromDataRow(DataRow row)
        {
            Entity.Landscape_ImageEntity entity = new Entity.Landscape_ImageEntity();
            entity.XCID = Convert.ToInt32(row["XCID"]);
            entity.JQID = Convert.ToInt32(row["JQID"]);
            if (Convert.IsDBNull(row["TPLJ"])) entity.TPLJ = null; else entity.TPLJ = row["TPLJ"].ToString();
            if (Convert.IsDBNull(row["JQMC"])) entity.JQMC = null; else entity.JQMC = row["JQMC"].ToString();

            return entity;
        }

        /// <summary>
        /// 转换SqlDataReader类型数据记录为实体
        /// </summary>
        private static Entity.Landscape_ImageEntity ConvertToEntityFromDataReader(SqlDataReader reader)
        {
            Entity.Landscape_ImageEntity entity = new Entity.Landscape_ImageEntity();
            entity.XCID = Convert.ToInt32(reader["XCID"]);
            entity.JQID = Convert.ToInt32(reader["JQID"]);
            if (Convert.IsDBNull(reader["TPLJ"])) entity.TPLJ = null; else entity.TPLJ = reader["TPLJ"].ToString();
            if (Convert.IsDBNull(reader["JQMC"])) entity.JQMC = null; else entity.JQMC = reader["JQMC"].ToString();

            return entity;
        }

    }
}
